Skip to content

Let a store seed retrieval and flag partial reads - #707

Merged
arrry-hydraDB merged 1 commit into
mainfrom
adityamishra/oss-136-store-hooks
Dec 4, 2026
Merged

arrry-hydraDB merged 1 commit into
mainfrom
adityamishra/oss-136-store-hooks

Conversation

@adityamishra-hydradb

@adityamishra-hydradb adityamishra-hydradb commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

1/5 of #706. A store that cannot list every entity can now seed retrieval with seed_candidates, and flag a read it cut short with mark_partial.
retrieve, neighbors, and find_path then report truncated. This PR also adds the three error codes the read-only Postgres store raises. Both SDKs.

Implementation details

Door: one-way
New public error codes (read_only_store, unsupported_read, graph_unavailable) and a new truncated field on the retrieve and path results.

Cleanup: none

What to delete:

Decisions

  • seed_candidates(ns, terms) is optional and found with getattr, like entities_matching and edge_versions. When a store has it, retrieve, search, and embedding seeds rank only its candidates with BM25. Other stores behave as before. _ops does not change.
  • mark_partial() lives in retrieval.py and is for edge reads only: node reads by id must still return every node that exists. It appends to a per-call list in a ContextVar (AsyncLocalStorage in TypeScript), so reads inside asyncio.gather still reach it. retrieve, neighbors, impact, and find_path set truncated on their result when a store called it.
  • truncated is left out of the retrieve and path output when it is false, so the output of every other store stays the same byte for byte.
  • find_path now traces the path through the entities both searches reached with the least total distance. Before, it assumed the two searches met at their last levels. That fails when a cut read makes the adjacency one-sided: _trace raised ValueError: min() iterable argument is empty. For complete reads the result is the same as before.
  • readiness reports a store that refuses full scans (unsupported_read) as ready and reachable, with that error and no counts. Before, an agent that called check_readiness first was told the graph was unreachable.
  • The error helpers (read_only_store, unsupported_read, graph_unavailable) are in this PR so the error docs and the every-code tests stay in parity in both SDKs. 2/5 and 5/5 replace the helper triggers with real store calls.

Validation

  • New tests, Python tests/test_store_read_hooks.py and TypeScript test/storeReadHooks.test.ts, use a wrapper over the Kuzu test store. They cover seeding from candidates, truncated on each walk result, the omitted field on a complete read, the early-meeting path, and readiness. The path test fails on main with the ValueError above.
  • Full Python suite on embedded Kuzu, A = main bb99998, B = top of the stack: A 1743 passed, 39 skipped; B 1750 passed, 48 skipped, 0 failed.
  • TypeScript lint, typecheck, build, and test with the conformance pair: A (main): lint, typecheck, and build pass; 1420 passed, 27 skipped. B (top of the stack): lint, typecheck, and build pass; 1427 passed, 36 skipped, 0 failed, conformance pair included; CI runs the same suites green on every PR. One earlier run of A timed out in conformanceGraph while Docker builds loaded the machine; the rerun passed.
  • python scripts/precheck.py --base origin/main, make probes, ruff check, ruff format --check: precheck --base origin/main: all checks passed. make probes: plan_apply, secret_echo, and unmerge_order pass; parity_table fails the same way on main and on the branch (vitest cannot parse a .scm query file in this environment). ruff check and ruff format --check: clean.
  • Raw outputs: selfgraph-oss136-proof/regression/ (local).

Limits

  • check_answerable still needs every entity and relation, so it raises unsupported_read on such a store.

Linked issue

Closes #706

AI Assistance

Model Effort
Claude Opus 5.5 High

Checklist

  • Implements an issue a maintainer has /approved.
  • Small and scoped to that issue.
  • New errors have a code and hint, the same on API, CLI, and MCP in both SDKs (see AGENTS.md).
  • Breaking changes described below, or none.

Breaking changes

None. _trace no longer takes hops; it is private.

🤖 Generated with Claude Code

@adityamishra-hydradb

Copy link
Copy Markdown
Collaborator Author

@hydra-mira-review review

@hydra-mira-review

hydra-mira-review Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Mira score 5/5 · 🟢 Good to merge

  • No findings in the code.
  • Finish the requirement: New hook tests cover truncation on every walk result.

Stores can supply retrieval candidates and report incomplete graph reads in both SDKs. Retrieval and graph walks expose truncated, and path tracing handles searches that meet before their final levels. The PR also adjusts readiness reporting and adds three store error codes.

How this PR works · 6 changes, 4 medium risk, 2 low risk

Stores that cannot list every entity need another way to seed retrieval and report incomplete edge reads. This PR adds optional store hooks in both SDKs and exposes partial results through truncated. It also prepares error codes and readiness behavior for the later read-only Postgres store.

1. Define errors for stores with restricted reads and writes · 🟢 low risk · src/selfgraph/errors.py +1

Callers can distinguish refused writes, unsupported reads, and unavailable graphs. New helpers return coded StoreError values with matching repair hints in both SDKs.

Risk: Incorrect codes or hints would prevent SDK clients and agents from distinguishing unsupported operations from unavailable stores.

Check:

  • Compare serialized helper errors across both SDKs, including a graph name containing an apostrophe.

In errors.py:58-63, errors.py:208-219, errors.py:488-516 and 2 more.

2. Seed retrieval from candidates returned by the store · 🟠 medium risk · typescript/src/protocols.ts +2

Entity seeds can come from a store-selected subset instead of a full scan. Lexical and embedding ranking use those candidates when the optional hook exists.

Risk: Incorrect candidate selection would silently omit relevant entity seeds for users of stores implementing the hook.

Check:

  • Return only Acme Labs from the hook and confirm retrieval excludes Acme while receiving sorted query terms.
  • Check entity search and embedding seeding with the hook, including type and historical visibility filters.

In protocols.ts:216-223, retrieval.py:557-626, retrieval.py:673-676 and 4 more.

3. Report incomplete edge reads on retrieval and walk results · 🟠 medium risk · src/selfgraph/models.py +4

Users can tell when a store returned incomplete edges. A store marker now sets truncated on retrieval, neighbors, impact, and path results.

Risk: Lost or leaked markers would make users treat incomplete results as complete or label an unrelated read as truncated.

Check:

  • Run concurrent complete and partial reads, including child edge-read tasks, and confirm their flags remain isolated.
  • Check impact results and confirm complete retrieval and path JSON omit truncated.

In models.py:1151-1168, models.py:1245-1260, models.ts:758-761 and 13 more.

4. Trace paths through the closest shared reached entities · 🟠 medium risk · src/selfgraph/retrieval.py +1

Path reconstruction handles searches that meet before their final levels. Previously, it assumed the meeting point matched the levels last expanded from both ends.

Risk: Incorrect meeting-point selection would return wrong steps or fail path requests for users in either SDK.

Check:

  • Check Ann to Bob through Acme when Acme's incoming read omits Bob; expect two steps and a partial result.
  • Compare complete-read paths with equal-length alternatives to confirm deterministic step ordering remains unchanged.

Why: A partial adjacency read can make the two searches observe different edges.

In retrieval.py:1872-1898, retrieval.py:1955-1957, retrieval.ts:2169-2203 and 1 more.

5. Treat refused full scans as ready and reachable · 🟠 medium risk · src/selfgraph/client.py +1

A store that refuses a readiness scan now reports ready and reachable. Previously, every StoreError made readiness report both values as false.

Risk: Incorrect error classification would let agents proceed against unusable stores or block supported reads.

Check:

  • Compare readiness responses for refused scans and connection failures; only refused scans should report ready and reachable.

Why: Agents checking readiness first can proceed with supported reads despite unavailable full scans.

In client.py:747-769, client.ts:475-510.

6. Supporting changes · 🟢 low risk · tests/test_store_read_hooks.py +9

Tests cover candidate seeding, partial responses, complete serialization, early path meetings, readiness, and error parity. Documentation describes the hooks and requires complete node reads by ID.

Risk: Incomplete regression coverage could allow SDK behavior or error contracts to diverge unnoticed.

Check:

  • Confirm the backend documentation clearly limits partial reads to edges.

In test_store_read_hooks.py:1-104, storeReadHooks.test.ts:1-132, test_errors.py:277-282 and 7 more.

Requirement check · 8 met, 1 partly met, 1 for another PR · from the PR description
  • ✅ Both SDKs optionally obtain entity seed candidates from the store and retain full scans for other stores (met). _entity_pages discovers the Python hook dynamically. entityPages checks the optional TypeScript hook before choosing the existing scan.
  • ✅ Retrieval, entity search, and embedding seeds operate on the store's candidates (met). Both lexical and embedding seed readers use candidate-aware entity paging. Entity search shares the seed pipeline.
  • ✅ Store markers propagate partial edge reads to retrieval, neighbors, impact, and path results (met). _flags_partial decorates all four Python reads. flagsPartial wraps all four TypeScript reads.
  • ✅ Complete retrieval and path responses omit the new field (met). Python result serializers remove false truncated values. TypeScript adds the optional property only when a read is marked partial.
  • ✅ Path reconstruction supports searches meeting before their last expanded levels (met). _trace and trace derive the meeting level from the least combined reached distance.
  • ✅ Readiness treats unsupported full scans as ready and reachable while retaining the error and omitting counts (met). Both SelfGraph.readiness implementations special-case unsupported_read and return empty counts.
  • ✅ Add matching restricted-store error codes, helpers, and hints in both SDKs (met). Both error modules define all three helpers and matching hints. Error tests and the conformance pair include their serialized outputs.
  • 🟡 New hook tests cover truncation on every walk result (partial). Both hook test files check retrieval, neighbors, and paths. Neither includes an impact assertion.
  • ✅ Answerability continues to require full entity and relation reads (met). check_answerable still scans entities and relations rather than using the new candidate hook.
  • ➖ Replace helper-triggered errors with real store calls in later stack work (for another PR). The description assigns those integrations to parts 2/5 and 5/5 of issue Read Postgres tables through pg_hydradb with a read-only store #706.
Diagram of the changed files
graph LR
  pyclient["src/selfgraph/client.py"] --> pyretrieval["src/selfgraph/retrieval.py"]
  pyretrieval --> pymodels["src/selfgraph/models.py"]
  tsclient["typescript/src/client.ts"] --> tsretrieval["typescript/src/retrieval.ts"]
  tsretrieval --> tsmodels["typescript/src/models.ts"]
Loading
⚠️ Other open PRs change the same code · 1

Merge them in order, or check that they agree.

  • #697 (merge-conflict risk). Context renderers and partial-read reporting both change retrieval result models and retrieval code in both SDKs, creating likely merge conflicts. Shared: docs/retrieval.md, src/selfgraph/client.py, src/selfgraph/models.py, src/selfgraph/retrieval.py, typescript/src/index.ts, typescript/src/models.ts, typescript/src/retrieval.ts, typescript/test/conformance/py_side.py, typescript/test/conformance/ts_side.ts

Comment @hydra-mira-review help for commands.

@adityamishra-hydradb

Copy link
Copy Markdown
Collaborator Author

@hydra-mira-review review

3 similar comments
@adityamishra-hydradb

Copy link
Copy Markdown
Collaborator Author

@hydra-mira-review review

@adityamishra-hydradb

Copy link
Copy Markdown
Collaborator Author

@hydra-mira-review review

@adityamishra-hydradb

Copy link
Copy Markdown
Collaborator Author

@hydra-mira-review review

@hydra-mira-review hydra-mira-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mira Review Summary

Partial chunk-node reads still crash Python retrieval instead of returning truncated context. Partial entity reads can also produce path steps without an entity in TypeScript, while the corresponding Python path handling raises an exception.

Key Issues

Issue Location
🔴 Partial chunk-node reads still abort retrieval before it can report truncation. src/selfgraph/retrieval.py:1393
🔴 Partial entity batches produce invalid path steps in TypeScript and an exception in Python. typescript/src/retrieval.ts:2320

Comment thread typescript/src/retrieval.ts Resolved
Comment thread src/selfgraph/retrieval.py Resolved
Signed-off-by: adityamishra-hydradb <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@adityamishra-hydradb
adityamishra-hydradb force-pushed the adityamishra/oss-136-store-hooks branch from 154ac9b to aa8da33 Compare December 4, 2026 22:00
@adityamishra-hydradb

Copy link
Copy Markdown
Collaborator Author

@hydra-mira-review review

1 similar comment
@adityamishra-hydradb

Copy link
Copy Markdown
Collaborator Author

@hydra-mira-review review

@arrry-hydraDB arrry-hydraDB left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@arrry-hydraDB
arrry-hydraDB merged commit cc8bae5 into main Dec 4, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Read Postgres tables through pg_hydradb with a read-only store

2 participants